package com.sys.app.entity.config;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 自定义数据库主键ID 继承IdentifierGenerator
 */
@Component
public class MyBatsPrimaryKeyConfig implements IdentifierGenerator  {
    //private final AtomicLong al = new AtomicLong(1);
    private final SnowFlake worker=new SnowFlake(1,1,1);
    @Override
    public Number nextId(Object entity) {
        return worker.nextId();
        //可以将当前传入的class全类名来作为bizKey,或者提取参数来生成bizKey进行分布式Id调用生成.
        //String bizKey = entity.getClass().getName();
        //MetaObject metaObject = SystemMetaObject.forObject(entity);
        //final long id = al.getAndAdd(1);
    }
}
